package controller

import (
	"context"
	"fmt"
	"html/template"
	"net/http"

	"jinse/common/handlers/log"
	"jinse/common/util"

	"jinse/idl/proto"
	exampleLogic "jinse/logic"
	"team.wphr.vip/technology-group/infrastructure/trace"
)

type ExampleController struct {
}

// Ping Ping
func (this ExampleController) Ping(ctx context.Context, req *proto.PingReq) (*proto.PingRsp, error) {
	resp, err := exampleLogic.ExampleLogicInstance.Ping(ctx, req.Name)

	if err != nil {
		log.Trace.Errorf(ctx, trace.DLTagUndefined, "msg=ping failed||errno=%d||err=%+v", util.GetErrno(err), err)
		return &proto.PingRsp{
			Errno:  util.GenOuterErrno(err),
			Errmsg: util.GenOuterErrmsg(err),
		}, nil
	}

	return &proto.PingRsp{
		Errno:  util.GenOuterErrno(err),
		Errmsg: util.GenOuterErrmsg(err),
		Data:   resp,
	}, nil
}

func (this ExampleController) ShowHtml(resp http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	if req.Method == "GET" {
		t, err := template.ParseFiles("html/index.html")
		if err != nil {
			fmt.Fprintf(resp, "parse template error: %s", err.Error())
			return
		}
		title := util.GetHttpQueryParam(req.Context(), "title")
		date := util.GetHttpQueryParam(req.Context(), "date")
		content := util.GetHttpQueryParam(req.Context(), "content")
		t.Execute(resp, map[string]string{
			"title":title,
			"date":date,
			"content":content,
		})
	}
	return
}

